<?php
namespace Admin\Controller;
use Think\Controller;

class RoleaddController extends CommonController {
    public function index(){
        
    	$cate = M('auth_category')->select();

    	$rule = M('auth_rule')->select();

    	foreach ($cate as $k => $v) {
    		foreach ($rule as $key => $value) {
    			if($v['cid']==$value['cid']){
    				$cate[$k]['content'][]=$rule[$key];
    			}
    		}
    	}
    	$this->assign('cate',$cate);
    	/*$sql = "select t1.*,t2.name,t2.title from auth_category t1 left join auth_rule t2 on t1.cid=t2.cid";

    	$cates = M()->query($sql);*/
    	/*echo "<pre>";
    	print_r($cate);
    	echo "</pre>";exit;
    	var_dump($cate);exit;

    	var_dump($rule);exit;*/

        $this->display();
    }

    public function add(){
    	$data = I('post.');
    	
    	for($i=0;$i<count($data['rule']);$i++){
    		$role[]=array('gname'=>$data['title'],'rid'=>$data['rule'][$i]);
    	}

    	$data['rules']=rtrim($data['rules'],',');

    	$result=M('auth_group')->add($data);

    	M('ruletogroup')->addAll($role);

    	if($result){
    		$this->ajaxReturn(['error'=>0,'info'=>'添加成功']);
    	}else{
    		$this->ajaxReturn(['error'=>1,'info'=>'添加失败']);
    	}
    }
}

?>